package cn.fl.capital.controller;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DecimalUtils;
import cn.fl.capital.constant.CapCashDepositDeductionConstant;
import cn.fl.capital.constant.CapitalConstant;
import cn.fl.capital.service.ICapFeeOutLogService;
import cn.fl.customer.service.ICstBscInfoService;
import cn.fl.customer.vo.CstBscInfoVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.project.condition.PrjPrdDtlQueryCondition;
import cn.fl.project.condition.PrjPrdPaySchDQueryCondition;
import cn.fl.project.constant.PrjPrdPaySchMConstant;
import cn.fl.project.service.IPrjBscInfoService;
import cn.fl.project.service.IPrjPrdDtlService;
import cn.fl.project.service.IPrjPrdPaySchDService;
import cn.fl.project.service.IPrjPrdPaySchMService;
import cn.fl.project.vo.*;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 客户资金表 控制层
 */
@Controller
@RequestMapping("/capital/cashDepositDeduction")
public class CapCashDepositDeductionController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(CapCashDepositDeductionController.class);

    /**
     * 客户 服务层BEAN
     */
    @Autowired
    private ICstBscInfoService cstBscInfoService;

    @Autowired
    private IPrjPrdPaySchMService prjPrdPaySchMService;

    @Autowired
    private IPrjPrdPaySchDService prjPrdPaySchDService;

    @Autowired
    private IPrjBscInfoService prjBscInfoService;

    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService ;

    @Autowired
    private ICapFeeOutLogService capFeeOutLogService;

    @Autowired
    private IPrjPrdDtlService prjPrdDtlService;




    /**
     * 显示保证金抵扣租金附加页面
     */
    @RequestMapping(value = "/toCashDepositDeductionPage.do", method = {RequestMethod.GET})
    public ModelAndView toCashDepositDeductionPage(HttpServletRequest request, HttpServletResponse response) {
        BaseLogUtils.newLogger("显示保证金抵扣租金附加页面." + CurrentThreadContext.getCurrentUserName()).info();
        String paySchDId = request.getParameter("paySchDId");//S01支付表明细ID
        //查询paySchId
        PrjPrdPaySchDQueryCondition prjPrdPaySchDQueryCondition = new PrjPrdPaySchDQueryCondition();
        prjPrdPaySchDQueryCondition.setFeeTypCd(CapitalConstant.CODE_FEE_TYP_CD_S01);
        prjPrdPaySchDQueryCondition.setId(Long.valueOf(paySchDId));
        List<PrjPrdPaySchDVO> prjPrdPaySchDVOS = this.prjPrdPaySchDService.searchList(prjPrdPaySchDQueryCondition);
        ModelAndView view = new ModelAndView("project/cashDepositDeduction/cashDepositDeductionPage");
        if(CollectionUtils.isNotEmpty(prjPrdPaySchDVOS)){
            Long paySchId = prjPrdPaySchDVOS.get(0).getPaySchId();
            //抵扣开始期次
            Integer startPd = prjPrdPaySchDVOS.get(0).getPayPd();
            PrjBscInfoVO prj = null;
            PrjPrdPaySchMVO pay = null;
            CstBscInfoVO cst = null;
            ListPageVO<PrjPrdPrpsFeeDetailVO> listPage = null;
            PrjPrdDtlVO dtl = null;
            //租金抵扣类型
            List<KeyValueVO> keyValueDTOS = sysDictionaryApiService.getKvList(CapCashDepositDeductionConstant.REP_0001, CapitalConstant.SYSCD,CapitalConstant.SYSORG);
            List<String> wsRepaymentInsConfig = new ArrayList<>();
            if(CollectionUtils.isNotEmpty(keyValueDTOS)){
                for(KeyValueVO keyValueVO : keyValueDTOS){
                    wsRepaymentInsConfig.add(keyValueVO.getKey());
                }
            }
            pay = prjPrdPaySchMService.selectById(Long.valueOf(paySchId));
            //是否显示付款 收款信息
            int showBk_oCIn = 0;
            //我司/代收代付
            String insDeduct = CapCashDepositDeductionConstant.INS_DEDUCT_NO;
            if(!wsRepaymentInsConfig.contains(pay.getBizFlag())){
                //资方划扣
                insDeduct = CapCashDepositDeductionConstant.INS_DEDUCT_YES;
                showBk_oCIn = 1;
            }
            //查询 S01-S04 计算客户剩余保证金
            Long feeTypO2 = 0L;
            if (PrjPrdPaySchMConstant.PAY_SCH_TYP_CD_ZF.equals(pay.getPaySchTypCd())) {
                feeTypO2 = capFeeOutLogService.calcuCapFeeAmt(Long.valueOf(paySchId),pay.getPaySchTypCd());
            }else {
                feeTypO2 = capFeeOutLogService.calcuCapFeeAmt(Long.valueOf(paySchId),pay.getPaySchTypCd());
            }
            pay = this.prjPrdPaySchMService.searchCashDepositDeductionPaySchD(Long.valueOf(paySchId),startPd);
            prj = this.prjBscInfoService.selectById(pay.getPrjId());
            cst = this.cstBscInfoService.selectDetailById(pay.getCstId());
            dtl = pay.getDtlVO();
            if (dtl == null) {
                PrjPrdDtlQueryCondition qc = new PrjPrdDtlQueryCondition();
                List<Long> prjPrdDtlIdList = new ArrayList<>();
                prjPrdDtlIdList.add(pay.getPrjPrdDtlId());
                qc.setIdList(prjPrdDtlIdList);
                List<PrjPrdDtlVO> dtlList = this.prjPrdDtlService.selectList(qc);
                if (CollectionUtils.isNotEmpty(dtlList)) {
                    dtl = dtlList.get(0);
                }
            }
            listPage = new ListPageVO<PrjPrdPrpsFeeDetailVO>(pay.getPrjPrdPayDetailList(), pay.getPrjPrdPayDetailList().size());
            pay.setPrjQuatPrpsPaySchPage(listPage);
            view.addObject("listPage", JSON.toJSONString(listPage));
            view.addObject("prj", prj);
            view.addObject("cst", cst);
            view.addObject("showBk_oCIn", showBk_oCIn);
            //是否抵扣页面
            view.addObject("cashDepositPage", 1);
            view.addObject("startPd", startPd);
            view.addObject("feeTypO2", DecimalUtils.convertFenToYuan(feeTypO2));
            view.addObject("insDeduct", insDeduct);
            view.addObject("pay", pay);
            view.addObject("dtl", dtl);
            // 返回已启用的支付表ID给页面
            view.addObject("paySchId", pay.getId());
            //是否资方支付表
            view.addObject("paySchTyp", pay.getPaySchTypCd());
            view.addObject("paySchDId",paySchDId);

        }else{
            BaseLogUtils.newLogger("租金表明细数据不存在.").setKeys("toCashDepositDeductionPage","paySchDId=" + paySchDId).info();

        }
        return view;

    }

}
